﻿using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using EmitMapper;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.JsonModel;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{
    [CustomAuthorize]
    public class LinksController : Controller
    {

        public IManagerLinks IMLinks { get; set; }

        /// <summary>
        /// 登录会话操作
        /// </summary>
        public Sessions Sessions { get; set; }
      
        public ActionResult Index(string tid)
        {
            ViewBag.TID = tid;
            return View();
        }

        [HttpPost]
        public JsonResult Links(string tid,int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;

            GridData<LinksVM> data = new GridData<LinksVM>();
            ObjectsMapper<LinksInfo, LinksVM> mapper =
                   ObjectMapperManager.DefaultInstance.GetMapper<LinksInfo, LinksVM>();

            var where = PredicateExtension.True<LinksInfo>();
            where = where.And(u => u.CompanyId == user.CompanyId && u.Types == tid);
            

            var count = IMLinks.LoadCount(where);
            var list = IMLinks.LoadAll(where, page, pageSize, new OrderByExpression<LinksInfo, int>(u => u.Id,true));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }

        public ActionResult Edit(string w,string tid,int Id = 0)
        {
            LinksVM link = new LinksVM();
            ObjectsMapper<LinksInfo, LinksVM> mapper =
                   ObjectMapperManager.DefaultInstance.GetMapper<LinksInfo, LinksVM>();

            if (Id > 0)
            {
                var res = IMLinks.GetLink(Id);
                link = mapper.Map(res);
            }
            else if(tid == "HT"){
                var res = IMLinks.GetLink(Sessions.User.CompanyId,tid);
                link = mapper.Map(res);
                if (link == null) {
                    link = new LinksVM()
                    {
                        CompanyId = 1,
                        Types = "HT"
                    };
                }
            }
            else
            {
                link.Types = tid;
            }
            if (string.IsNullOrEmpty(link.ImgUrl)) {
                link.ImgUrl = "/images/nothing.jpg";
            }
            ViewBag.W = string.IsNullOrEmpty(w) ? "200" : w;
            return View(link);
        }

        [HttpPost]
        public ActionResult Edit(LinksVM av)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<LinksVM, LinksInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<LinksVM, LinksInfo>();

                var user = Sessions.User;

                if (av.Id == 0)
                {
                    var a = mapper.Map(av);
                    a.CompanyId = user.CompanyId;
                    bool res = IMLinks.AddLink(a);
                    return Json(res);
                }
                else
                {
                    var a = IMLinks.GetLink(av.Id);
                    a.ImgUrl = av.ImgUrl;
                    a.Title = av.Title;
                    a.CompanyId = user.CompanyId;
                    a.Url = av.Url;

                    bool res = IMLinks.UpdateLink(a);
                    return Json(res);
                }

            }

            return View();
        }

        [HttpPost]
        public ActionResult DeleteLinks(string ids)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var item in idList)
                {
                    list.Add(Convert.ToInt32(item));
                }

                b = IMLinks.DeleteLink(list);
            }
            return Json(b);
        }

    }
}
